#include "mountdelegate.h"
#include "../CommunicateDelegate/communicatenormaldelegate.h"
#include "hmicommondefine.h"
#include "instructionEnum.h"


#define D_MOUNT_TRY_MAX_COUNT   10

MountDelegate::MountDelegate(CommunicateNormalDelegate *communicateNormalIn, GlobalData *globalDataIn)
{
    communicateNormal=communicateNormalIn;
    globalData=globalDataIn;
    qDebug()<<"thread id:"<<QThread::currentThreadId()<< " mount init";

    recordLoginIP = "";

    timer = new QTimer;
    timeoutCount = 0;
    connect(timer,SIGNAL(timeout()),this,SLOT(timeoutSlot()));
}

MountDelegate::~MountDelegate()
{
//    qDebug()<<"*****MountDelegate():umount*****";
//    if(0==system("umount /agvwork/AgvFundation/fundation/data"))
//    {
//        qDebug()<<"*****umount ok*****";
//    }
//    else
//    {
//        qDebug()<<"*****umount false*****";
//    }
}


void MountDelegate::timeoutSlot()
{
    timeoutCount++;

    if(30 == timeoutCount)
    {
        emit msgBoxSignal(EM_MSG_BOX_NO_BTN,tr("提示"),tr("文件挂载超时\n无法使用,请尝试重启"));
    }
}

void MountDelegate::mountFileAddress_linux(QStringList list, QString mountPath)
{
    // 挂载文件已废弃
    return;

    //debug用，不mount
    return;
    QSettings settings(CTRL_IP_INI_PATH, QSettings::IniFormat);
    int isMount = settings.value("Login/isMount").toInt();
    if(0==isMount)
    {
        if(globalData)
        {
            globalData->mountSuccess = true;
        }
        return;
    }


    //date -s "2012-08-27 15:04:00";
    qDebug()<<"thread id:"<<QThread::currentThreadId()<<" mount start";
    //执行机器人程序 路径挂载
    //QStringList list = globalData->serverIP.split(":");
    if(list.size() == 2)
    {
        if( hmiIp == list.at(0) ){
            if(globalData)
            {
                globalData->mountSuccess = true;
            }
            return;
        }

        QString tmpCmd ="mount -t nfs "+ list[0]+":"+mountPath + " " + mountPath + " -o tcp,nolock";//
        QByteArray tmpCmdArray = tmpCmd.toLatin1();
        QString umountCmd="umount "+mountPath;
        QByteArray tmpUmountCmd = umountCmd.toLatin1();
        if(recordLoginIP != list[0])//2017.06.26添加
        {
            timer->start(1000);                         //1s 一次 计时器

            system( tmpUmountCmd.data() );
            qDebug()<<"umount ok";

            int mountTryCount=0;
            while(D_MOUNT_TRY_MAX_COUNT>mountTryCount)
            {
                if(0 == system(tmpCmdArray.data()))
                {
                    qDebug()<<"***mount ok!***"<<endl;

                    timer->stop();
                    timeoutCount = 0;//清零//因为存在角色切换的情况, 会重新mount

                    recordLoginIP = list[0];//缓存
                    if(globalData)
                    {
                        //globalData->lock();
                        globalData->mountSuccess = true;
                        //globalData->unlock();
                    }
                    break;
                }
                mountTryCount++;
                int returnFlag;
                communicateNormal->restartNfsService(returnFlag);
                sleep(1);

            }
            if(D_MOUNT_TRY_MAX_COUNT==mountTryCount)
            {
                qDebug()<<"***mount error!***"<<endl;
                timer->stop();
                timeoutCount = 0;//清零
                emit msgBoxSignal(EM_MSG_BOX_NO_BTN,tr("提示"),tr("文件挂载失败\n无法使用,请尝试重启"));
            }
        }
        else
        {
            if(globalData)
            {
                globalData->mountSuccess = true;
            }
        }
    }
}

void MountDelegate::mountFileAddress_windows(QStringList list, QString mountPath)
{
    if(globalData)
    {
        globalData->mountSuccess = true;
    }
}

void MountDelegate::mountFileAddress(QStringList list,QString mountPath)
{
#ifdef BUILD_X86_WINDOWS
    mountFileAddress_windows(list,mountPath);
#else
    mountFileAddress_linux(list,mountPath);
#endif

}
